package cn.com.dingtek.genius.manager.server.web.controller;

import cn.com.dingtek.genius.manager.server.repository.db.entity.UserLoginLog;
import cn.com.dingtek.genius.manager.server.repository.db.entity.UserOperateLog;
import cn.com.dingtek.genius.manager.server.service.LogService;
import cn.com.dingtek.genius.manager.server.support.sam.annotation.AssertAuth;
import cn.com.dingtek.genius.manager.server.web.Result;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;

    @AssertAuth
    @PostMapping("/user_login/list")
    public Result userLoginLogList(@RequestBody JSONObject json) {
        int currentPage = json.getInteger("current_page") == null ? 0 : json.getInteger("current_page");
        int pageSize = json.getInteger("page_size") == null ? 10 : json.getInteger("page_size");
        UserLoginLog param = json.getObject("search_param", UserLoginLog.class);
        return Result.ok(logService.userLoginLogList(param, currentPage, pageSize));
    }

    @AssertAuth
    @PostMapping("/user_login/delete")
    public Result userLoginDelete(@RequestBody JSONObject json) {
        List<Long> ids = json.getJSONArray("data").toJavaList(Long.class);
        if (ids == null || ids.isEmpty()) {
            return Result.ok();
        }
        logService.userLoginDelete(ids);
        return Result.ok();
    }


    @AssertAuth
    @PostMapping("/user_login/download")
    public void userLoginDownload(@RequestBody JSONObject json, HttpServletResponse httpServletResponse) throws IOException {
        List<Long> ids = json.getJSONArray("data").toJavaList(Long.class);
        if (ids == null || ids.isEmpty()) {
            return;
        }
        logService.userLoginDownload(ids, httpServletResponse);
    }


    @AssertAuth
    @GetMapping("/user_login/day/download")
    public void userLoginDownload(@RequestParam Integer day, HttpServletResponse httpServletResponse) throws IOException {
        logService.userLoginDayDownload(day, httpServletResponse);
    }

    @AssertAuth
    @PostMapping("/user_operate/list")
    public Result userOperateLogList(@RequestBody JSONObject json) {
        int currentPage = json.getInteger("current_page") == null ? 0 : json.getInteger("current_page");
        int pageSize = json.getInteger("page_size") == null ? 10 : json.getInteger("page_size");
        UserOperateLog param = json.getObject("search_param", UserOperateLog.class);
        return Result.ok(logService.userOperateLogList(param, currentPage, pageSize));
    }

    @AssertAuth
    @PostMapping("/user_operate/delete")
    public Result userOperateDelete(@RequestBody JSONObject json) {
        List<Long> ids = json.getJSONArray("data").toJavaList(Long.class);
        if (ids == null || ids.isEmpty()) {
            return Result.ok();
        }
        logService.userOperateDelete(ids);
        return Result.ok();
    }


    @AssertAuth
    @PostMapping("/user_operate/download")
    public void userOperateDownload(@RequestBody JSONObject json, HttpServletResponse httpServletResponse) throws IOException {
        List<Long> ids = json.getJSONArray("data").toJavaList(Long.class);
        if (ids == null || ids.isEmpty()) {
            return;
        }
        logService.userOperateDownload(ids, httpServletResponse);
    }


    @AssertAuth
    @GetMapping("/user_operate/day/download")
    public void userOperateDownload(@RequestParam Integer day, HttpServletResponse httpServletResponse) throws IOException {
        logService.userOperateDownload(day, httpServletResponse);
    }
}
